package cn.tannn.surenessdemo.controller;

import cn.jdevelops.annotation.mapping.PathRestController;
import cn.jdevelops.entity.basics.vo.SerializableVO;
import cn.jdevelops.result.result.ResultVO;
import cn.tannn.surenessdemo.controller.dto.AddAuthRouteDTO;
import cn.tannn.surenessdemo.controller.dto.UpdateAuthRouteDTO;
import cn.tannn.surenessdemo.controller.vo.AuthRouteVO;
import cn.tannn.surenessdemo.user.entity.AuthRoute;
import cn.tannn.surenessdemo.user.service.AuthRouteService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Optional;

/**
 * 路由接口
 * 1. 新增路由（不给客户开放
 * 2. 删除路由（不给客户开放
 * 3. 查询路由树
 * 4. 听启用路由树（拦截，不拦截
 * 5. 编辑路由树
 * 6. 搜索
 *
 * @author tnnn
 * @version V1.0
 * @date 2022-07-28 10:17
 */
@PathRestController
@Slf4j
@Api(tags = "路由管理", value = "路由管理")
@RequiredArgsConstructor
@ApiSupport(order = 10)
public class AuthRouteController {

    private final AuthRouteService authRouteService;


    @ApiOperation(value = "新增路由", notes = "路由管理")
    @PostMapping("addRoute")
    @ApiOperationSupport(order = 1)
    public ResultVO<String> addRoute(@RequestBody @Validated AddAuthRouteDTO addAuthRoute){
        if(authRouteService.addAuthRoute(addAuthRoute.to(AuthRoute.class))){
            return ResultVO.fail("新增失败");
        }else {
            return ResultVO.success();
        }
    }


    @ApiOperation(value = "更新路由", notes = "路由管理")
    @PostMapping("updateRoute")
    @ApiOperationSupport(order = 2)
    public ResultVO<String> updateRoute(@RequestBody @Validated UpdateAuthRouteDTO addAuthRoute){
        if(authRouteService.updateAuthRoute(addAuthRoute.to(AuthRoute.class))){
            return ResultVO.fail("更新失败");
        }else {
            return ResultVO.success();
        }
    }


    @ApiOperation(value = "删除路由", notes = "路由管理")
    @PostMapping("deleteRoute")
    @ApiOperationSupport(order = 3)
    public ResultVO<String> deleteRoute(@RequestBody  @NotEmpty List<String> routeCode){
        if(authRouteService.deleteAuthRoute(routeCode)){
            return ResultVO.fail("删除失败");
        }else {
            return ResultVO.success();
        }
    }

    @ApiOperation(value = "查询所有路由", notes = "路由管理")
    @GetMapping("findAll")
    @ApiOperationSupport(order = 4)
    public ResultVO<List<AuthRouteVO>> findAll(){
        Optional<List<AuthRoute>> authRoutesOp = authRouteService.getAuthRoutesOp();
        return authRoutesOp.map(roules -> ResultVO.successForData(SerializableVO.to(roules, AuthRouteVO.class)))
                .orElseGet(() -> ResultVO.fail("暂无路由"));
    }




}
